package com.cheyun.netsalev3.widget

import android.app.Dialog
import android.os.Bundle
import android.view.*
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.databinding.MyContentDialogLayout2Binding
import kotlin.reflect.KFunction1
import android.view.LayoutInflater



class MyContentDialogLayout : DialogFragment() {
    lateinit var binding: MyContentDialogLayout2Binding
    /**系统调用这个来获得对话片段的布局，不管是否显示为对话框或嵌入片段。*/
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        //将布局膨胀为对话框或嵌入片段
        binding = DataBindingUtil.inflate(inflater, R.layout.my_content_dialog_layout2, container, false)
        val view = inflater.inflate(content, null)
        initView()
        binding.conetenView.addView(view)
        binding.setLifecycleOwner(this)

        return binding.root
    }


    private fun initView() {
        binding.cancelButton.setOnClickListener {
            dismiss()
            onClickCancel?.apply { this() }
        }
        binding.okButton.setOnClickListener {
            dismiss()
            onClickOk?.apply {
                this()
            }
        }
//        binding.conetenTex.text = content
        binding.titleText.text = title



        callbackInitView?.apply {
            this(binding)
        }

        setCancelable(touchCancel)
    }


    /**系统仅在对话框中创建布局时调用此操作。*/
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        val window = dialog.window
        window.setBackgroundDrawableResource(android.R.color.transparent)

//        if(touchCancel == false){
//            dialog.setCanceledOnTouchOutside(false)
//        }
        return dialog
    }

    var title = ""
    var content = 0
    var touchCancel = true
    var onClickOk: (() -> Unit)? = null
    var onClickCancel: (() -> Unit)? = null
    var callbackInitView: KFunction1<@ParameterName(name = "binding") MyContentDialogLayout2Binding, Unit>? = null

//    fun setUnTouchCancel(){
//        getDialog()?.setCanceledOnTouchOutside(false)
//        getDialog()?.setOnKeyListener()
//        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
//            @Override
//            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//                if (keyCode == KeyEvent.KEYCODE_BACK) {
//                    return true;
//                }
//                return false;
//            }
//        });
//    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            title = it.getString("title")
            content = it.getInt("content")
            touchCancel = it.getBoolean("touchCancel")
        }
    }

    companion object {
        @JvmStatic
        fun newInstance(title: String, content: Int, touchCancel:Boolean = true) =
                MyContentDialogLayout().apply {
                    arguments = Bundle().apply {
                        putString("title", title)
                        putInt("content", content)
                        putBoolean("touchCancel", touchCancel)
                    }
                }
    }


}